<?php
/*
 * Created on 2016/11/19/
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 session_start();
 require_once('../db/properties.inc.php');
 $userInfo = $_SESSION["userInfo"]; 
 if(!$userInfo){
 	header("Location: ../../view/login.html");
 	exit;
 }
 $title = $_POST["headingt"];//标题
 $content = $_POST["content"];//内容
 $types = $_POST["types"];//类型
 $blogId = $_POST["blogId"];
 $userObj = json_decode($userInfo);//用户信息
 $nowDate =  date('Y-m-d H:i:s',time());//当前时间
 
 if(!$title||!$content){
 	header("Location: ../../view/editcon.php?staus=1");
 	exit;
 }
 
 try {
	   //保存博文
		 $conn = new mysqli($url, $user, $pwd, $dbname, $port);
		 $conn -> set_charset("utf8");
		 $conn -> autocommit(FALSE);
		 
		 if (mysqli_connect_errno()) {
		 	header("Location: ../../view/editcon.php?staus=1");
		    exit;
		 }
		$sqlTitleUp = "UPDATE bolggerTitle SET title=?,types=?,updateTime=? WHERE id=?"; 
		$sqlContentUp = "UPDATE bolggerContent SET content=? WHERE titleId=?";
		$sqlTitle = "INSERT INTO bolggerTitle (title,userId,types,addTime) VALUES(?,?,?,?)";
		$sqlContent = "INSERT INTO bolggerContent (titleId, content) VALUES(?,?)";
		if(empty($blogId)){
			//插入主表
			$mysqli_stmt=$conn->prepare($sqlTitle);
			//第一个参数表明变量类型，有i(int),d(double),s(string),b(blob)
			$mysqli_stmt->bind_param('siss',$title,$userObj->id,$types,$nowDate);
			//执行预处理语句
			$mysqli_stmt->execute();
			$titleId = $mysqli_stmt->insert_id;
			//插入附表  
			$mysqli_stmt1=$conn->prepare($sqlContent);
			$mysqli_stmt1->bind_param('is',$titleId,$content);
			$mysqli_stmt1->execute();
		}else{
			$mysqli_stmt=$conn->prepare($sqlTitleUp);
			//第一个参数表明变量类型，有i(int),d(double),s(string),b(blob)
			$mysqli_stmt->bind_param('sssi',$title,$types,$nowDate,$blogId);
			//执行预处理语句
			$mysqli_stmt->execute();
			//插入附表  
			$mysqli_stmt1=$conn->prepare($sqlContentUp);
			$mysqli_stmt1->bind_param('si',$content,$blogId);
			$mysqli_stmt1->execute();
		}
		
		$conn->commit();
		$conn->close();
		//echo $content;
		header("Location: ../../index.php");
		exit;
	}catch( Exception $e ) {
		header("Location: ../../view/editcon.php?staus=1");
		exit;
	}
?>
